﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace GrouponDesktop.AbmCliente
{
    public partial class ModifCliente : Form
    {
        private int idCli;
        private DataGridView dgv_ciudades = new DataGridView();

        public ModifCliente(int id)
        {
            InitializeComponent();

            DataGridViewColumn ciudades = new DataGridViewColumn();
            DataGridViewCell cell = new DataGridViewTextBoxCell();
            ciudades.CellTemplate = cell;
            ciudades.Name = "ciudades";
            ciudades.ValueType = typeof(string);
            dgv_ciudades.Columns.Add(ciudades);

            tb_fechaNac.Format = DateTimePickerFormat.Custom;
            tb_fechaNac.CustomFormat = "yyyy-MM-dd";
            
            this.idCli = id;
          

            string text = BDConnection.QueryAndExecuteScalarString("select nombre from CodingSpree.TL_CLIENTES where id_cliente = " + this.idCli);
            tb_nombre.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("select apellido from CodingSpree.TL_CLIENTES where id_cliente = " + this.idCli);
            tb_apellido.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("select dni from CodingSpree.TL_CLIENTES where id_cliente = " + this.idCli);
            tb_dni.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("select mail from CodingSpree.TL_CLIENTES where id_cliente = " + this.idCli);
            tb_mail.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("select telefono from CodingSpree.TL_CLIENTES where id_cliente = " + this.idCli);
            tb_telefono.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("SELECT replace(convert(varchar, fecha_nacimiento, 111), '/', '-') from CodingSpree.TL_CLIENTES where id_cliente = " + this.idCli);
            tb_fechaNac.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("select calle from CodingSpree.TL_CLIENTES where id_cliente = " + this.idCli);
            tb_dire.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("select piso from CodingSpree.TL_CLIENTES where id_cliente = " + this.idCli);
            tb_piso.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("select departamento from CodingSpree.TL_CLIENTES where id_cliente = " + this.idCli);
            tb_dept.Text = text;
            
            text = BDConnection.QueryAndExecuteScalarString("select cod_postal from CodingSpree.TL_CLIENTES where id_cliente = " + this.idCli);
            tb_cp.Text = text;

            cb_local.Text = BDConnection.QueryAndExecuteScalarString("select ciudad from CodingSpree.TL_CIUDADES a, CodingSpree.TL_CLIENTES b where a.id_ciudad = b.id_ciudad and b.id_cliente = " + idCli);

            BDConnection.FillComboBox(cb_local, "select ciudad from CodingSpree.TL_CIUDADES");
        }

        private void b_modif_Click(object sender, EventArgs e)
        {
         
                string query = "EXEC CodingSpree.updateCliente ";
                query = query + idCli + ", '";
                query = query + tb_nombre.Text + "', '";
                query = query + tb_apellido.Text + "', '";
                query = query + tb_dni.Text + "', '";
                query = query + tb_mail.Text + "', '";
                query = query + tb_telefono.Text + "', '";
                query = query + tb_fechaNac.Text + "', '";
                query = query + tb_dire.Text + "', '";
                query = query + tb_piso.Text + "', '";
                query = query + tb_dept.Text + "', '";
                query = query + tb_cp.Text + "', '";
                query = query + cb_local.Text + "'";


                BDConnection.execute(query);


                for (int i = 0; i < (this.dgv_ciudades.Rows.Count - 1); i++)
                {

                    query = "EXEC CodingSpree.insertCiudXCli '";
                    query = query + dgv_ciudades.Rows[i].Cells[0].Value.ToString() + "'";

                    BDConnection.execute(query);

                }

                if (cb_habil.Checked == true)
                {
                    BDConnection.execute("EXEC CodingSpree.altaLogCli " + idCli);
                }

                MessageBox.Show("Modificacion Realizada");
                this.Close();
            

        }

       

        private void b_select_Click_1(object sender, EventArgs e)
        {
            BuscarReg br = new BuscarReg(this.dgv_ciudades, this.tb_ciudades);
            br.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string user = BDConnection.QueryAndExecuteScalarString("EXEC CodingSpree.getUserCli " + idCli);
            CambiarContraseña CC = new CambiarContraseña(user);
            CC.Show();
        }

    }
}
